const express = require('express');
const request = require('request'); // 使用request模块发送HTTP请求
const app = express();

app.use(express.json());

app.post('/login', (req, res) => {
  const { code } = req.body;
  const appId = '你的AppID';
  const appSecret = '你的AppSecret';
  const url = `https://api.weixin.qq.com/sns/jscode2session?appid=${appId}&secret=${appSecret}&js_code=${code}&grant_type=authorization_code`;

  request(url, { json: true }, (error, response, body) => {
    if (!error && response.statusCode === 200) {
      // 成功获取openid和session_key
      const { openid, session_key } = body;
      // 进行后续的用户登录处理
      // 例如，创建会话，返回会话标识等
      res.send({ success: true, openid, session_key });
    } else {
      res.status(500).send({ success: false, message: 'Failed to get user info' });
    }
  });
});

app.listen(3000, () => {
  console.log('Server is running on port 3000');
});